<template>
	<view>
		<custom-navigation-bar title="账号申请"></custom-navigation-bar>
		<view class="main">
			
			<view class="form-item" v-if="data.oldUsername!=null" >
				<view class="form-item-1">
					<view class="form-item-lable">
						原账号
					</view>
					<view class="form-item-countent" style="line-height: 90rpx;">
						{{data.oldUsername}}
					</view>
					<view class="form-item-right">
						<image src="/static/gengduo@3x(1).png" class="form-item-image"></image>
					</view>
				</view>
			</view>
			
			<view class="form-item">
				<view class="form-item-1">
					<view class="form-item-lable">
						新账号
					</view>
					<view class="form-item-countent">
						<input v-model="data.username" type="text" class="form-item-input" placeholder="请输入新账号"  />
					</view>
					<view class="form-item-right">
						<image src="/static/gengduo@3x(1).png" class="form-item-image"></image>
					</view>
				</view>
			</view>
			  
			<view class="form-item">
				<view class="form-item-1" style="border: none;margin-top: 80rpx;margin-bottom: 80rpx;">
					<view class="btn" @click="setUserName">确认修改</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavigationBar from '@/components/CustomNavigationBar.vue';
	import http from '../../../utils/http';
	
	 
	const data=ref({
		openId:getApp().globalData.user.openid,
		oldUsername:getApp().globalData.user.username,
		username:''
	});
	function setUserName(){
		uni.showLoading({
			title: '设置中',
			mask:true
		}); 
		http.request({
		 	url: "/my/user/install/username",
		 	method: "POST",
		 	data: {
		 		"userName":data.value.username,
				"openId":data.value.openId
		 	}
		}).then((responseData) => { 
			uni.hideLoading();
			if(responseData.code==1){
				getApp().globalData.user.username=data.value.username;
				uni.navigateBack()
			}else if(responseData.code==-1||responseData.code==-2){
				uni.navigateTo({
					url: "/pages/login/login"
				});
			}else{
				uni.showToast({
					icon: 'error',
					title: responseData.message
				})
			}
		}).catch((err) => {
			uni.hideLoading();
		 	uni.showToast({
		 		icon: 'error',
		 		title: '请求失败'
		 	})
		})
	}
</script>

<style>
	@import "../../application/form.scss";
</style>